﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace ScreenShark
{
    public partial class SplashForm : Form
    {
        private const int WS_EX_NOACTIVATE = 0x08000000;
        private const int WS_EX_TOOLWINDOW = 0x00000080;

        private Color _FillColor = SystemColors.ControlDarkDark;

        public SplashForm()
        {
            InitializeComponent();                        
        }

        private bool IsMouseAbove { get; set; }
        private GraphicsPath Path { get; set; }
        
        public String LabelText
        {
            get { return textLabel.Text; }
            set { textLabel.Text = value; }
        }

        public Color FillColor
        {
            get { return _FillColor; }
            set
            {
                if (_FillColor != value)
                {
                    _FillColor = value;

                    this.Refresh();
                }
            }
        }

        private void SplashForm_Load(object sender, EventArgs e)
        {
            var wa = Screen.PrimaryScreen.WorkingArea;
            Left = wa.X+(wa.Width - Width) / 2;
            Top = wa.Y + (wa.Height - wa.Height/7 - Height/2);
            Path = RoundedRectangle.Create(this.ClientRectangle, 10, RoundedRectangle.RectangleCorners.All);

            fadeInTimer.Enabled = true;
        }

        protected override bool ShowWithoutActivation
        {
            get { return true; }
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams baseParams = base.CreateParams;

                baseParams.ExStyle |= WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW;

                return baseParams;
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            using (var b = new SolidBrush(FillColor))
            {
                e.Graphics.FillPath(b, Path);
            }            
        }

        private bool Fade(double inc)
        {
            if ((inc > 0 && Opacity < 1.0-inc) || (inc < 0.0 && Opacity+inc > 0.0))
            {
                Opacity += inc;
                return true;
            }

            return false;
        }

        private void SplashForm_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                SharkApp.Instance.GoToLastScreenshot();             
            }
            
            Close();
        }

        private void SplashForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {

            }
        }

        private void SplashForm_MouseEnter(object sender, EventArgs e)
        {

        }

        private void fadeOutTimer_Tick(object sender, EventArgs e)
        {
            if (!Fade(-0.1))
            {
                Close();
            }
        }

        private void fadeInTimer_Tick(object sender, EventArgs e)
        {
            if (!Fade(0.1))
            {
                fadeInTimer.Enabled = false;
                aliveTimer.Enabled = true;
            }
        }

        private void aliveTimer_Tick(object sender, EventArgs e)
        {
            aliveTimer.Enabled = false;
            fadeOutTimer.Enabled = true;
        }

        private void goToToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void clipboardToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void SplashForm_DoubleClick(object sender, EventArgs e)
        {

        }

        private void SplashForm_Activated(object sender, EventArgs e)
        {

        }

    }
}
